/**
 * 
 */
package com.ekingstar.framework.security;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 *
 */
public class StatckHashMapUtil{
	
	public static Object get(Object val,ValueStack stack){
		if (stack == null) return val;
		if (val instanceof String){
			String tval = (String)val;
			if (tval.startsWith("${") && tval.endsWith("}")){
				tval = tval.substring(2, tval.length()-1);
				return stack.findValue(tval,String[].class);
			}else{
				return val;
			}
			
		}else if (val instanceof String[]) {
			String[] sval = (String[])val;
			List res = new ArrayList();
			for (int i = 0;i < sval.length;i++){
				String tval = sval[i];
				if (tval.startsWith("${") && tval.endsWith("}")){
					tval = tval.substring(2, tval.length()-1);
					Object stackVal = stack.findValue(tval);
					if (stackVal != null ){
						if (stackVal instanceof String){
							res.add(stackVal);
						}else if (stackVal instanceof Collection){
							res.addAll((Collection)stackVal);
						}else if (stackVal instanceof String[]){
							String[] t_stackVal = (String[])stackVal;
							List t_list = Arrays.asList(t_stackVal);
							res.addAll(t_list);
						}
					}
				}else{
					res.add(tval);
				}
			}
			return res.toArray();
		}else {
			return val;
		}
	}
}
